﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Nasa8x.Web.UI
{
    [ToolboxData("<{0}:PageHeader runat=\"server\"></{0}:PageHeader>")]
    public class PageHeader : System.Web.UI.WebControls.PlaceHolder
    {
        public string Title
        {
            get;
            set;
        }

        public string Keywords
        {
            get;
            set;
        }

        public string Description
        {
            set;
            get;
        }

        public string[] StyleSheets
        {
            get;
            set;
        }


        public string[] JavaScripts
        {
            get;
            set;
        }




        public string ShortcutIcon
        {
            get;
            set;
        }



        // public string BaseUrl { set; get; }

        public string CssBaseUrl
        {
            get;
            set;
        }

        public string JsBaseUrl
        {
            get;
            set;
        }


        private static string GetMetaDate()
        {
            DateTime now = DateTime.Now - TimeSpan.FromDays(365);
            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

            // example of format:  1994-11-06T08:49:37+00:00

            string date = now.ToString("yyyy-MM-ddTHH:mm:sszzz", dtfi);

            return date;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Page.Header.Title = Title;

        }


        protected override void Render(HtmlTextWriter output)
        {
            HtmlMeta meta;
            if (!string.IsNullOrEmpty(Description))
            {
                meta = new HtmlMeta();
                meta.Name = "description";
                meta.Content = Description;
                Controls.Add(meta);
            }


            meta = new HtmlMeta();
            meta.Name = "keywords";
            meta.Content = Keywords;
            Controls.Add(meta);

            meta = new HtmlMeta();
            meta.Name = "date";
            meta.Content = GetMetaDate();
            Controls.Add(meta);


            //Forum Shortcut Icon 

            if (!string.IsNullOrEmpty(ShortcutIcon))
            {
                Controls.Add(
                new LiteralControl(
                    string.Format("<link rel=\"shortcut icon\" href=\"{0}\" type=\"image/x-icon\" />",
                                  ShortcutIcon)));
            }


            // stylesheets
            if (StyleSheets != null && StyleSheets.Length > 0)
            {
                //string[] sheets = StyleSheets.Split(',');
                // string cssUrl = string.Format("{0}/themes/{1}/css.axd?f={2}", SiteUrl, Skin, StyleSheets);

                foreach (string css in StyleSheets)
                {
                    if (!string.IsNullOrEmpty(css))
                    {
                        HtmlLink link = new HtmlLink();
                        link.Href = CssBaseUrl + css;
                        link.Attributes.Add("rel", "stylesheet");
                        link.Attributes.Add("type", "text/css");
                        Controls.Add(link);
                    }
                }

            }

            if (JavaScripts != null && JavaScripts.Length > 0)
            {

                foreach (string js in JavaScripts)
                {
                    if (!string.IsNullOrEmpty(js))
                    {
                        string _script = String.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", JsBaseUrl + js);
                        Controls.Add(new LiteralControl(_script));

                    }
                }


            }


            base.Render(output);
        }


    }
}
